var mysql = require('mysql');
var config = require('../config/index.js')

var pool  = mysql.createPool({
    host     : config.db.HOST,
    user     : config.db.USERNAME,
    password : config.db.PASSWORD,
    database : config.db.DATABASE,
    port     : config.db.PORT
});

let query = ( sql, values ) => {

    return new Promise(( resolve, reject ) => {
        pool.getConnection( (err, connection) => {
            if (err) {
                reject( err )
            } else {
                connection.query(sql, values, ( err, rows) => {
                    if ( err ) {
                        reject( err )
                    } else {
                        resolve( rows )
                    }
                    connection.release()
                })
            }
        })
    })

}

let users =
    `create table if not exists users(
     id INT NOT NULL AUTO_INCREMENT,
     name VARCHAR(100) NOT NULL COMMENT '用户名',
     pass VARCHAR(100) NOT NULL COMMENT '密码',
     avator VARCHAR(100) NOT NULL COMMENT '头像',
     moment VARCHAR(100) NOT NULL COMMENT '注册时间',
     PRIMARY KEY ( id )
    );`

let createTable = ( sql ) => {
    return query( sql, [] )
}
createTable(users)
// 注册用户
exports.insertData = ( value ) => {
    console.log(pool)
    console.log(value)
    console.log(config)

    // let _sql = "insert into users set name=?,pass=?,avator=?,moment=?;"
    // return query( _sql, value )
}